package com.asas.spaceflight.view;

import java.util.Iterator;

import com.asas.spaceflight.model.Asteroid;
import com.asas.spaceflight.model.Bullet;
import com.asas.spaceflight.model.PhysicalEntity;
import com.asas.spaceflight.model.Player;
import com.asas.spaceflight.model.World;
import com.asas.spaceflight.model.Player.State;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.GL10;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.glutils.ShapeRenderer;
import com.badlogic.gdx.graphics.glutils.ShapeRenderer.ShapeType;
import com.badlogic.gdx.math.Polygon;
import com.badlogic.gdx.math.Rectangle;
import com.badlogic.gdx.math.Vector2;

public class WorldRenderer {

	private World world;
	private Player player;
	
	private PlayerRender playerRender;	
	private SpriteBatch batch;
	
	public static int WIDTH = 480;
	public static int HEIGHT = 320;

	// DEBUG
	private ShapeRenderer shapeRenderer;
	
	public WorldRenderer(World world) {
		this.world = world;
		player = world.getPlayer();
		playerRender = new PlayerRender(player);
		batch = new SpriteBatch();
		shapeRenderer = new ShapeRenderer();
	}
	
	public void render() {
		Gdx.gl.glClearColor(0.1f, 0.1f, 0.1f, 1);
		Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
		
		playerRender.update();
		
		// DEBUG
		debugAsteroids();

		// Sprites
		batch.begin();
		playerRender.render(batch);
		batch.end();
		
		// DEBUG
		debugBullets();
		debugVectors();
		debugFrames();
	}
	
	public void debugAsteroids() {
		
		Iterator<Asteroid> iterator = world.getAsteroids().iterator();
		while (iterator.hasNext()) {
			Asteroid obj = iterator.next();
			
			Color color;
			switch (obj.getType()) {
			case BrideCake: color = Color.WHITE; break;
			case BrideCakePiece: color = Color.GRAY; break;
			default: color = Color.GREEN; break;
			}
			
			drawCircle(obj.getPosition(), obj.getRadius(), color);
		}
	}	
	public void debugBullets() {
		Iterator<Bullet> iterator = world.getBullets().iterator();
		while (iterator.hasNext()) {
			Bullet obj = iterator.next();
			drawCircle(obj.getPosition(), obj.getRadius(), Color.YELLOW);
		}
	}	
	private void drawArrow(Vector2 from, Vector2 to, Color color) {
		shapeRenderer.begin(ShapeType.Line);
		shapeRenderer.setColor(color);
		shapeRenderer.line(from.x, from.y, to.x, to.y);
		shapeRenderer.end();		
		shapeRenderer.begin(ShapeType.FilledCircle);
		shapeRenderer.setColor(color);
		shapeRenderer.filledCircle(from.x, from.y, 2);
		shapeRenderer.filledCircle(to.x, to.y, 4);
		shapeRenderer.end();		
	}
	private void drawCircle(Vector2 pos, float radius, Color color) {
		shapeRenderer.begin(ShapeType.FilledCircle);
		shapeRenderer.setColor(color);
		shapeRenderer.filledCircle(pos.x, pos.y, radius);
		shapeRenderer.end();
	}
	private void drawRect(Polygon rect, Color color) {
		float[] v = rect.getTransformedVertices();
		shapeRenderer.begin(ShapeType.Line);
		shapeRenderer.setColor(color);
		shapeRenderer.line(v[0], v[1], v[2], v[3]);
		shapeRenderer.line(v[2], v[3], v[4], v[5]);
		shapeRenderer.line(v[4], v[5], v[6], v[7]);
		shapeRenderer.line(v[6], v[7], v[0], v[1]);
		shapeRenderer.end();	
	}
	@SuppressWarnings("unused")
	private void drawRect(Rectangle area, Color color) {
		shapeRenderer.begin(ShapeType.FilledCircle);
		shapeRenderer.setColor(color);
		shapeRenderer.filledRect(area.x, area.y, area.width, area.height);
		shapeRenderer.end();
	}

	// DEBUG
	private void debugVectors() {		
		Iterator<Asteroid> iterator = world.getAsteroids().iterator();
		while (iterator.hasNext()) {
			drawVectors(iterator.next());
		}
		drawVectors(player);
	}
	private void debugFrames() {		
		Iterator<Asteroid> iterator = world.getAsteroids().iterator();
		while (iterator.hasNext()) {
			drawRect(iterator.next().getPolygonalFrame(), Color.YELLOW);
		}
		drawRect(player.getPolygonalFrame(),
				player.getState().equals(State.DYING)
					? Color.RED : Color.YELLOW);
	}
	private void drawVectors(PhysicalEntity entity) {
		float toPixel = 8f;
		Vector2 pos = entity.getPosition().cpy();
		Vector2 velocity = entity.getVelocity().cpy();
		
		if (velocity.len() != 0) {
			velocity.mul(toPixel).add(pos);
			drawArrow(pos, velocity, Color.BLUE);
		}
	}

}
